package leetcode.primary;

import utils.linkedList.LinkedList;
import utils.linkedList.ListNode;

public class T61 {
    public ListNode rotateRight(ListNode head, int k) {
        if (k==0 || head==null || head.next==null){
            return head;
        }
        int len=1;
        ListNode node = head;
        while(node.next!=null){
            len++;
            node = node.next;
        }


        int m =len-k%len;
        int i =1;
        ListNode last = node;
        node = head;
        while (i!=m){
            node = node.next;
            i++;
        }
        last.next = head;
        head = node.next;
        node.next = null;

        return head;
    }

    public static void main(String[] args) {
        T61 test = new T61();
        LinkedList linkedList = new LinkedList();
        ListNode head = linkedList.initLinkedList(new int[]{1});
        ListNode node = test.rotateRight(head, 2);
        while (node!=null){
            System.out.print(node.val+" ");
            node = node.next;
        }
    }
}


